// OutStreamWithCRC.h

#ifndef __OUT_STREAM_WITH_CRC_H
#define __OUT_STREAM_WITH_CRC_H

#include "../C/7zCrc.h"

#include "Common/Stream/IStream.h"

namespace Common
{
namespace Archive
{

	//used only in ArchiveExtractCallback in both GUI and FM
class ATL_NO_VTABLE COutStreamWithCRC :
	public CComObjectRootEx<CComSingleThreadModel>,
  public ISequentialOutStream 
{
  CComPtr<ISequentialOutStream> _stream;
  UINT64 _size;
  UINT32 _crc;
  bool _calculate;
public:
	BEGIN_COM_MAP(COutStreamWithCRC)
		COM_INTERFACE_ENTRY(ISequentialOutStream)
	END_COM_MAP()


	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		return S_OK;
	}

	void FinalRelease()
	{
	}
  STDMETHOD(Write)(const void *data, UINT32 size, UINT32 *processedSize);
  void SetStream(ISequentialOutStream *stream) { _stream = stream; }
  void ReleaseStream() { _stream.Release(); }
  void Init(bool calculate = true)
  {
    _size = 0;
    _calculate = calculate;
    _crc = CRC_INIT_VAL;
  }
  void InitCRC() { _crc = CRC_INIT_VAL; }
  UINT64 GetSize() const { return _size; }
  UINT32 GetCRC() const { return CRC_GET_DIGEST(_crc); }
};

}
}

#endif
